﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Models;
using WindowsFormsApp1.Respostories;
using static System.Windows.Forms.AxHost;

namespace WindowsFormsApp1
{
    public partial class UserForm : Form
    {
        public UserForm()
        {
            InitializeComponent();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

                /// <summary>
        /// 添加序号列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView datagridview = sender as DataGridView;
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y,
            datagridview.RowHeadersWidth - datagridview.RowHeadersWidth / 2 + 6,
            e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                datagridview.RowHeadersDefaultCellStyle.Font,
                rectangle,
                datagridview.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }


        /// <summary>
        /// 启动DataGridView双缓存，解决数据量大时出现加载延迟
        /// </summary>
        /// <param name="view"></param>
        public void EnableDoubleBuffer(DataGridView view)
        {
            Type dgvType = view.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
                BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(view, true, null);
        }



        private void UserForm_Load(object sender, EventArgs e)
        {
            // 设置DataGridView不自动生成数据
            this.dataGridView1.AutoGenerateColumns = false;
            //启动设置DataGridView双缓存
            this.EnableDoubleBuffer(this.dataGridView1);

            // 绑定数据源
            List<User> users = UserRespostoriy.GetUsers();
            this.dataGridView1.DataSource = users ;



        }

        private void btNew_Click(object sender, EventArgs e)
        {
            //打开编辑窗口
            EditUserFrom editUserFrom = new EditUserFrom();
            editUserFrom.ShowDialog();
            //刷新数据
            if (editUserFrom.DialogResult == DialogResult.OK)
            {
                //重新绑定数据源
                this.dataGridView1.DataSource = UserRespostoriy.GetUsers();
            }


        }


        /// <summary>
        /// 点击单元格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取当前行数据
            User user = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as User;

            //获取当前列索引
            int columnIndex = e.ColumnIndex;

            //判断是否是倒数第三列,进行密码重置操作
            if (columnIndex == this.dataGridView1.ColumnCount-3)
            {
                //重置密码操作
                //弹出提示框
                DialogResult result = MessageBox.Show("确定要重置密码吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //重置密码
                    if (UserRespostoriy.ResetPassword(user))
                    {
                        //弹出提示框
                        MessageBox.Show("密码重置成功！\n"+user.Mobile.Substring(user.Mobile.Length - 4), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //弹出提示框
                        MessageBox.Show("密码重置失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    //刷新数据
                    this.dataGridView1.DataSource = UserRespostoriy.GetUsers();
                }

            }

            //判断是否是倒数第二列,.进行编辑操作
            if (columnIndex == this.dataGridView1.ColumnCount - 2)
            {
                //编辑操作
                //弹出编辑窗口
                //user=
                EditUserFrom editUserFrom = new EditUserFrom(user);
                editUserFrom.ShowDialog();
                //刷新数据
                if (editUserFrom.DialogResult == DialogResult.OK)
                {
                    //重新绑定数据源
                    this.dataGridView1.DataSource = UserRespostoriy.GetUsers();
                }


            }

            //判断是否是倒数第一列,进行删除操作
            if (columnIndex == this.dataGridView1.ColumnCount - 1)
            {
                //删除操作
                DialogResult result = MessageBox.Show("确定要删除该用户吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //删除用户
                    if (UserRespostoriy.DeleteUser(user))
                    {
                        //弹出提示框
                        MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //弹出提示框
                        MessageBox.Show("删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    //刷新数据
                    this.dataGridView1.DataSource = UserRespostoriy.GetUsers();
                }
            }
        }


        /// <summary>
        /// 点击表格文字内容事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        /// <summary>
        /// 点击查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btQuery_Click(object sender, EventArgs e)
        {
            //获取查询条件
            string user = "";
            if (this.cbUser.Text != "")
            {
                user = this.cbUser.Text;
                if (user == "所有用户")
                    user = "";
            }
            string key = "";
            if (this.txtKey.Text != "")
            {
                key = this.txtKey.Text;
            }

            int state = -1;
            if (this.cbState.Text=="启用")
            {
               state=1;
            }
            else if (this.cbState.Text == "禁用")
            {
                state = 0;
            }
            

            List<User> users = UserRespostoriy.QueryUsers(user, key, state);
            //更新数据元组
            this.dataGridView1.DataSource = users;
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            //清空查询条件
            this.cbUser.Text = "";
            this.txtKey.Text = "";
            this.cbState.Text = "";

            List<User> users = UserRespostoriy.GetUsers();
            //更新数据元组
            this.dataGridView1.DataSource = users;
        }
    }
}
